home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / whttpd14.zip / SUPPORT / LOGCYCLE / LOGCYCLE.C < prev    next >
C/C++ Source or Header  |  1994-06-05  |  4KB  |  160 lines

  1. //tabs=4
  2. //
  3. //        ==========
  4. //        LOGCYCLE.C
  5. //        ==========
  6. //
  7. // Utility to signal Windows httpd to cycle its logfile(s). See comments
  8. // at SigHTTPD() below. I also produced a DLL that does this for use with
  9. // Visual Basic, etc.
  10. //
  11. // Author:    Robert B. Denny
  12. //            <rdenny@netcom.com>
  13. //            05-Jun-94
  14. //
  15. // History:
  16. //
  17. // Who               When        What
  18. // ----------------- ---------   ---------------------------------------
  19. // Robert B. Denny   05-Jun-94   Created - First release
  20. //
  21. //----------------------------------------------------------------------
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. #include "getopt.h"
  28.  
  29. #define VERSION "V1.0/06-Jun-94"
  30.  
  31. extern HINSTANCE _hInstance;        // BC4 has this in library
  32.  
  33. HWND hTheWindow;                // Used by WindCatcher() during enumeration
  34.  
  35. static BOOL SigHTTPD(int mask);
  36. static void usage(void);
  37. BOOL CALLBACK WindCatcher(HWND hWnd, LPARAM lparam);
  38.  
  39. // ====
  40. // MAIN
  41. // ====
  42. main(int argc, char *argv[])
  43. {
  44.     int c;
  45.     WORD mask = 0;
  46.  
  47.     while((c = getopts(argc, argv, "aeh")) != -1)
  48.     {
  49.         switch(tolower(c))
  50.         {
  51.           case 'a':                            // Cycle access log
  52.             mask |= 1;
  53.             break;
  54.  
  55.           case 'e':
  56.             mask |= 2;
  57.             break;
  58.  
  59.           case '?':
  60.           case 'h':
  61.             usage();
  62.         }
  63.     }
  64.  
  65.     if(mask == 0)                            // Default to cycle access log only
  66.         mask = 1;
  67.  
  68.     if(!SigHTTPD(mask))
  69.     {
  70.         printf("It doesn't appear that the Web server 'httpd' is running.\n");
  71.         usage();
  72.     }
  73.  
  74.     return(0);
  75. }
  76.  
  77. //--------------------------------------------------------------------------
  78. //
  79. // SigHTTPD() - Signal Windows httpd to cycle log(s).
  80. //
  81. // Enumerates windows looking for one whose name starts with 'httpd'. Once
  82. // found, it posts a (WM_USER + 13) message to that window. The wParam of
  83. // the message tells which logfiles to cycle (mask):
  84. //
  85. //    wParam = 1        Cycle access log
  86. //    wParam = 2        Cycle Error log
  87. //    wParam = 3        Cycle both logs
  88. //
  89. // Returns non-zero (-1, true) if successful, else 0 (false)
  90. //--------------------------------------------------------------------------
  91. static BOOL SigHTTPD(int mask)
  92. {
  93.     FARPROC lpfnEnumWinCB;            // Thunk for enum callback
  94.  
  95.     hTheWindow = NULL;                // Assume failure
  96.  
  97.     lpfnEnumWinCB = MakeProcInstance((FARPROC)WindCatcher, _hInstance);
  98.     EnumWindows(lpfnEnumWinCB, NULL);
  99.     FreeProcInstance(lpfnEnumWinCB);
  100.  
  101.     if(hTheWindow != NULL)            // If we found an httpd running
  102.     {
  103.         //
  104.         // NOTE: This is hard-coded for httpd V1.2b8.
  105.         //
  106.         return(PostMessage(hTheWindow, (WM_USER + 13), (WPARAM)mask, 0));
  107.     }
  108.     return(0);
  109. }
  110.  
  111.  
  112. //------------------------------------------------------------------------
  113. //
  114. // WindCatcher() - Callback function for EnumTaskWindows(). Passes
  115. //                 back a copy of the next window handle.
  116. //
  117. //------------------------------------------------------------------------
  118. #define BUF_LEN 64
  119. #pragma argsused
  120. BOOL CALLBACK WindCatcher (HWND hWnd, LPARAM lparam)
  121. {
  122.     char buf[BUF_LEN+1];
  123.     static const char *tgtName = "httpd";
  124.     register int i;
  125.  
  126.     GetWindowText(hWnd, (LPSTR)buf, BUF_LEN);    // Get name
  127.     for(i=0; i < 5; i++)                        // Avoid huge C library
  128.         if(buf[i] != tgtName[i])
  129.             break;
  130.     if(i == 5)
  131.     {                                            // Found it
  132.         hTheWindow = hWnd;
  133.         return(FALSE);
  134.     }
  135.     return(TRUE);                                // Keep going
  136. }
  137.  
  138. //------------------------------------------------------------------------
  139. //
  140. // Usage() - Display usage information then exit.
  141. //
  142. //------------------------------------------------------------------------
  143. static void usage(void)
  144. {
  145.     printf("\nlogcycle %s\nusage:\n", VERSION);
  146.     printf("    logcycle [-a] [-e] [-h]\n\n");
  147.     printf("-a  Cycle the access log\n");
  148.     printf("-e  Cycle the error log\n");
  149.     printf("-h  Show this information (also '-?')\n\n");
  150.     printf("If no options given, cycles only the access log.\n");
  151.     printf("NOTE: The server must be running for this to work.\n\n");
  152.     printf("Active log(s) renamed as \".001\", and previous old log(s) renamed\n");
  153.     printf("from \".001\" to \".002\" and so forth up to 30 cycles. The\n");
  154.     printf("oldest is deleted.\n");
  155.  
  156.     exit(-1);
  157. }
  158.  
  159.  
  160.